local xiane = fk.CreateSkill {
  name = "th_xiane",
}

xiane:addEffect(fk.EventPhaseProceeding, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xiane.name) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.dead then return end
    local num = #player:getTableMark("th_xiane-turn") + 1
    local choiceprey = "#th_xiane-choiceprey"
    local choiceheal = "#th_xiane-choiceheal"
    local choiceplay = "#th_xiane-choiceplay"
    local all_choices = {choiceheal, choiceprey, choiceplay}
    local choices = table.slice(all_choices, 1, num + 1)
    local getter = table.filter(room:getOtherPlayers(player), function(p)
      return not p:isNude() end)
    if #getter == 0 then
      table.removeOne(choices, choiceprey)
    end
    while #choices > 0 do
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = xiane.name,
        cancelable = false,
        all_choices = all_choices
      })
      if choice == choiceprey then
        local to = room:askToChoosePlayers(player, {
          targets = getter,
          min_num = 1,
          max_num = 1,
          prompt = "#th_xiane-choicepreyask",
          skill_name = xiane.name,
          cancelable = false
        })
        if #to == 1 then
          room:doIndicate(player, to)
          local id = room:askToChooseCard(player, {
            target = to[1],
            flag = "he",
            skill_name = xiane.name
          })
          room:obtainCard(player, id, false, fk.ReasonPrey)
        end
      elseif choice == choiceheal then
        player:drawCards(1, xiane.name)
      elseif choice == choiceplay then
        player:gainAnExtraPhase(Player.Play)
      end
      table.removeOne(choices, choice)
      if player.dead then return end
    end
  end,
})
xiane:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data.card and not table.contains(player:getTableMark("th_xiane-turn"), data.card.color)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("th_xiane-turn")
    table.insert(mark, data.card.color)
    room:setPlayerMark(player, "th_xiane-turn", mark)
  end
})

Fk:loadTranslationTable{
  ["th_xiane"] = "显厄",
  [":th_xiane"] = '结束阶段，你可以依次执行前X项（X为你本回合使用过牌的颜色数+1）：<br>' ..
  '1.摸一张牌；<br>' ..
  '2.获得一名其他角色一张牌；<br>' ..
  "3.此阶段结束后执行一个额外的出牌阶段。",

  ["#th_xiane-choiceprey"] = "获得一名其他角色一张牌",
  ["#th_xiane-choicepreyask"] = "显厄：你可以获得一名其他角色区域内的一张牌",

  ["#th_xiane-choiceheal"] = "摸一张牌",

  ["#th_xiane-choiceplay"] = "此阶段结束后执行一个额外的出牌阶段",
}

return xiane